import logging
from pathlib import Path
from app.config import settings

def setup_logger(name: str) -> logging.Logger:
    """配置日志器"""
    logger = logging.getLogger(name)
    # 配置日志格式和输出
    
    logger.setLevel(settings.LOG_LEVEL)
    
    # 创建日志目录
    log_dir = Path("logs")
    log_dir.mkdir(exist_ok=True)
    
    # 文件处理器
    file_handler = logging.FileHandler(
        log_dir / f"{name}.log",
        encoding="utf-8"
    )
    file_handler.setFormatter(
        logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        )
    )
    
    # 控制台处理器
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(
        logging.Formatter(
            "%(levelname)s: %(message)s"
        )
    )
    
    logger.addHandler(file_handler)
    logger.addHandler(console_handler)
    
    return logger 